import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.WindowConstants;
import javax.swing.JProgressBar;
import javax.swing.SwingConstants;
import java.awt.Color;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JTabbedPane;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;
import java.awt.Font;
import java.util.Calendar;
import java.util.LinkedList;

import javax.swing.JComboBox;
import javax.swing.JSeparator;
import javax.swing.JButton;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;


public class ServerWindow {

	protected volatile IServer correspondingServer = null;
	protected volatile SystemPowerState sps = null;
	protected volatile IServerClusterUsageData sC_UsageData = null;
	
	private LinkedList<IJob> unservicedJobListCopy = null;
	private LinkedList<IServicedJob> runningJobListCopy = null;
	
	private String serverSpecificConsoleText = "";
	
	private JFrame frmServerWindow;
	private JProgressBar progressBar;
	private JTextField text_ServerID;
	private JTextField text_ServerNPU;
	private JTextField text_ServerCores;
	private JTextField text_ServerPf;
	private JTextField text_ServerPc;
	private JTextField text_ServerMaxPower;
	private JTextField text_ServerPowAvg;
	private JTextField text_ServerCap;
	private JTextArea text_ServerDesc;
	private JTextField text_jobsDue;
	private JTextField text_jobsRunning;
	private JTextArea serverSpecificConsole;
	private JTextField text_ServerMemory;
	private JTextField text_jobName;
	private JTextField text_jobMemReq;
	private JComboBox comboBox_unservicedJobs;
	private JTextField text_jobDuration;
	private JTextField text_jobPriority;
	private JTextField text_jobNPE;
	private JTextField text_jobStartTime;
	private JTextField text_jobEndTime;
	private JTextField text_jobPc;
	private JTextArea text_JobDesc;
	private JComboBox comboBox_runningJobs;
	private JButton btnShowJob_unserviced;
	private JButton btnShowJob_running;
	private JTextField text_ServerFreeMemory;

	/**
	 * Launch the application.
	 */
	public void startWindow() {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					frmServerWindow.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public ServerWindow(IServer server, SystemPowerState sps, IServerClusterUsageData sC_UsageData) 
	{
		this.correspondingServer = server;
		this.sps = sps;
		this.sC_UsageData = sC_UsageData;
		
		unservicedJobListCopy = new LinkedList<IJob>();
		runningJobListCopy = new LinkedList<IServicedJob>();
		
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frmServerWindow = new JFrame();
		frmServerWindow.getContentPane().setBackground(new Color(222, 184, 135));
		frmServerWindow.setTitle("Server Window: [Server " + correspondingServer.getServerID() + "]");
		frmServerWindow.setBounds(100, 100, 570, 470);
		frmServerWindow.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		
		progressBar = new JProgressBar();
		progressBar.setToolTipText("Power Consumption as a percentage total consumable power for this server");
		progressBar.setBounds(6, 23, 26, 408);
		
		Runnable progressBarValue = new Runnable()
		{
			public void run()
			{
				while(true)
				{
					progressBar.setValue((int) sps.getValue(correspondingServer.getServerID()));
					
					try {
						Thread.sleep(1000);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			}
		};
		
		Thread progressBarUpdate = new Thread(progressBarValue);
		progressBarUpdate.start();
		
		frmServerWindow.getContentPane().setLayout(null);
		
		progressBar.setBackground(Color.WHITE);
		progressBar.setForeground(Color.BLACK);
		progressBar.setStringPainted(true);
		progressBar.setOrientation(SwingConstants.VERTICAL);
		progressBar.setMaximum((int) correspondingServer.getMaxPower());
		frmServerWindow.getContentPane().add(progressBar);
		
		JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
		tabbedPane.setBounds(44, 6, 520, 436);
		frmServerWindow.getContentPane().add(tabbedPane);
		
		JPanel panel = new JPanel();
		panel.setBackground(new Color(143, 188, 143));
		tabbedPane.addTab("Basic", null, panel, null);
		panel.setLayout(null);
		
		JLabel lblServerId = new JLabel("Server ID: ");
		lblServerId.setBounds(47, 39, 70, 16);
		panel.add(lblServerId);
		
		JLabel lblServerDescription = new JLabel("Server Description: ");
		lblServerDescription.setBounds(47, 319, 123, 16);
		panel.add(lblServerDescription);
		
		JLabel lblServerProcessorCount = new JLabel("No of Processors: ");
		lblServerProcessorCount.setBounds(47, 67, 123, 16);
		panel.add(lblServerProcessorCount);
		
		JLabel lblProcessorCoreCount = new JLabel("No of Cores/Processor:");
		lblProcessorCoreCount.setBounds(47, 95, 161, 16);
		panel.add(lblProcessorCoreCount);
		
		JLabel lblCurrentPowerConsumption = new JLabel("Current Power Consumption (Watt-Hour):");
		lblCurrentPowerConsumption.setBounds(47, 151, 260, 16);
		panel.add(lblCurrentPowerConsumption);
		
		JLabel lblMaximumConsumablePower = new JLabel("Maximum Consumable Power (Appx.):");
		lblMaximumConsumablePower.setToolTipText("Maximum Consumable Power (Theoretical)");
		lblMaximumConsumablePower.setBounds(47, 179, 238, 16);
		panel.add(lblMaximumConsumablePower);
		
		JLabel lblAveragePowerConsumption = new JLabel("Average Power Consumption (Watt-Hour): ");
		lblAveragePowerConsumption.setBounds(47, 207, 267, 16);
		panel.add(lblAveragePowerConsumption);
		
		JLabel lblServercapabilityValue = new JLabel("Server \"Capability\" Factor: ");
		lblServercapabilityValue.setBounds(47, 235, 181, 16);
		panel.add(lblServercapabilityValue);
		
		JLabel lblServerPowerFactor = new JLabel("Server Power Factor:");
		lblServerPowerFactor.setBounds(47, 123, 134, 16);
		panel.add(lblServerPowerFactor);
		
		text_ServerID = new JTextField();
		text_ServerID.setBounds(322, 33, 171, 28);
		text_ServerID.setHorizontalAlignment(SwingConstants.CENTER);
		text_ServerID.setText("0");
		text_ServerID.setForeground(new Color(255, 255, 255));
		text_ServerID.setBackground(new Color(106, 90, 205));
		text_ServerID.setEditable(false);
		panel.add(text_ServerID);
		text_ServerID.setColumns(10);
		
		text_ServerNPU = new JTextField();
		text_ServerNPU.setBounds(322, 61, 171, 28);
		text_ServerNPU.setText("0");
		text_ServerNPU.setHorizontalAlignment(SwingConstants.CENTER);
		text_ServerNPU.setForeground(Color.WHITE);
		text_ServerNPU.setEditable(false);
		text_ServerNPU.setColumns(10);
		text_ServerNPU.setBackground(new Color(106, 90, 205));
		panel.add(text_ServerNPU);
		
		text_ServerCores = new JTextField();
		text_ServerCores.setBounds(322, 89, 171, 28);
		text_ServerCores.setText("0");
		text_ServerCores.setHorizontalAlignment(SwingConstants.CENTER);
		text_ServerCores.setForeground(Color.WHITE);
		text_ServerCores.setEditable(false);
		text_ServerCores.setColumns(10);
		text_ServerCores.setBackground(new Color(106, 90, 205));
		panel.add(text_ServerCores);
		
		text_ServerPf = new JTextField();
		text_ServerPf.setBounds(322, 117, 171, 28);
		text_ServerPf.setText("0");
		text_ServerPf.setHorizontalAlignment(SwingConstants.CENTER);
		text_ServerPf.setForeground(Color.WHITE);
		text_ServerPf.setEditable(false);
		text_ServerPf.setColumns(10);
		text_ServerPf.setBackground(new Color(106, 90, 205));
		panel.add(text_ServerPf);
		
		text_ServerPc = new JTextField();
		text_ServerPc.setBounds(322, 145, 171, 28);
		text_ServerPc.setText("0");
		text_ServerPc.setHorizontalAlignment(SwingConstants.CENTER);
		text_ServerPc.setForeground(Color.WHITE);
		text_ServerPc.setEditable(false);
		text_ServerPc.setColumns(10);
		text_ServerPc.setBackground(new Color(106, 90, 205));
		panel.add(text_ServerPc);
		
		text_ServerMaxPower = new JTextField();
		text_ServerMaxPower.setBounds(322, 173, 171, 28);
		text_ServerMaxPower.setText("0");
		text_ServerMaxPower.setHorizontalAlignment(SwingConstants.CENTER);
		text_ServerMaxPower.setForeground(Color.WHITE);
		text_ServerMaxPower.setEditable(false);
		text_ServerMaxPower.setColumns(10);
		text_ServerMaxPower.setBackground(new Color(106, 90, 205));
		panel.add(text_ServerMaxPower);
		
		text_ServerPowAvg = new JTextField();
		text_ServerPowAvg.setBounds(322, 201, 171, 28);
		text_ServerPowAvg.setText("0");
		text_ServerPowAvg.setHorizontalAlignment(SwingConstants.CENTER);
		text_ServerPowAvg.setForeground(Color.WHITE);
		text_ServerPowAvg.setEditable(false);
		text_ServerPowAvg.setColumns(10);
		text_ServerPowAvg.setBackground(new Color(106, 90, 205));
		panel.add(text_ServerPowAvg);
		
		text_ServerCap = new JTextField();
		text_ServerCap.setBounds(322, 229, 171, 28);
		text_ServerCap.setText("0");
		text_ServerCap.setHorizontalAlignment(SwingConstants.CENTER);
		text_ServerCap.setForeground(Color.WHITE);
		text_ServerCap.setEditable(false);
		text_ServerCap.setColumns(10);
		text_ServerCap.setBackground(new Color(106, 90, 205));
		panel.add(text_ServerCap);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(322, 319, 171, 65);
		panel.add(scrollPane);
		
		text_ServerDesc = new JTextArea();
		scrollPane.setViewportView(text_ServerDesc);
		text_ServerDesc.setText("My First Server");
		text_ServerDesc.setEditable(false);
		text_ServerDesc.setForeground(new Color(255, 255, 255));
		text_ServerDesc.setBackground(new Color(106, 90, 205));
		
		JLabel lblServerBasicInformation = new JLabel("Basic Server Information");
		lblServerBasicInformation.setFont(new Font("Lucida Grande", Font.BOLD, 13));
		lblServerBasicInformation.setBounds(162, 6, 171, 16);
		panel.add(lblServerBasicInformation);
		
		JLabel lblTotalServerMemory = new JLabel("Total Server Memory (KB):");
		lblTotalServerMemory.setBounds(47, 263, 161, 16);
		panel.add(lblTotalServerMemory);
		
		text_ServerMemory = new JTextField();
		text_ServerMemory.setText("0");
		text_ServerMemory.setHorizontalAlignment(SwingConstants.CENTER);
		text_ServerMemory.setForeground(Color.WHITE);
		text_ServerMemory.setEditable(false);
		text_ServerMemory.setColumns(10);
		text_ServerMemory.setBackground(new Color(106, 90, 205));
		text_ServerMemory.setBounds(322, 257, 171, 28);
		panel.add(text_ServerMemory);
		
		JLabel lblAvailableServerMemory = new JLabel("Available Server Memory (KB):");
		lblAvailableServerMemory.setBounds(47, 291, 191, 16);
		panel.add(lblAvailableServerMemory);
		
		text_ServerFreeMemory = new JTextField();
		text_ServerFreeMemory.setText("0");
		text_ServerFreeMemory.setHorizontalAlignment(SwingConstants.CENTER);
		text_ServerFreeMemory.setForeground(Color.WHITE);
		text_ServerFreeMemory.setEditable(false);
		text_ServerFreeMemory.setColumns(10);
		text_ServerFreeMemory.setBackground(new Color(106, 90, 205));
		text_ServerFreeMemory.setBounds(322, 285, 171, 28);
		panel.add(text_ServerFreeMemory);
		
		startBasicInfoThread();		// Start the Basic Info Thread
		
		JPanel panel_1 = new JPanel();
		panel_1.setBackground(new Color(143, 188, 143));
		tabbedPane.addTab("Specific", null, panel_1, null);
		panel_1.setLayout(null);
		
		JLabel lblJobsDueProcessing = new JLabel("Unprocessed Jobs:");
		lblJobsDueProcessing.setBounds(6, 43, 124, 16);
		panel_1.add(lblJobsDueProcessing);
		
		text_jobsDue = new JTextField();
		text_jobsDue.setText("0");
		text_jobsDue.setHorizontalAlignment(SwingConstants.CENTER);
		text_jobsDue.setForeground(Color.WHITE);
		text_jobsDue.setEditable(false);
		text_jobsDue.setColumns(10);
		text_jobsDue.setBackground(new Color(106, 90, 205));
		text_jobsDue.setBounds(138, 37, 90, 28);
		panel_1.add(text_jobsDue);
		
		JLabel lblRunningJobs = new JLabel("Running Jobs:");
		lblRunningJobs.setBounds(301, 43, 90, 16);
		panel_1.add(lblRunningJobs);
		
		text_jobsRunning = new JTextField();
		text_jobsRunning.setText("0");
		text_jobsRunning.setHorizontalAlignment(SwingConstants.CENTER);
		text_jobsRunning.setForeground(Color.WHITE);
		text_jobsRunning.setEditable(false);
		text_jobsRunning.setColumns(10);
		text_jobsRunning.setBackground(new Color(106, 90, 205));
		text_jobsRunning.setBounds(403, 37, 90, 28);
		panel_1.add(text_jobsRunning);
		
		JLabel lblServerDetailedInformation = new JLabel("Detailed Server Information");
		lblServerDetailedInformation.setFont(new Font("Lucida Grande", Font.BOLD, 13));
		lblServerDetailedInformation.setBounds(150, 6, 192, 16);
		panel_1.add(lblServerDetailedInformation);
		
		JScrollPane scrollPane_1 = new JScrollPane();
		scrollPane_1.setBounds(6, 98, 487, 286);
		panel_1.add(scrollPane_1);
		
		serverSpecificConsole = new JTextArea();
		serverSpecificConsole.setEditable(false);
		scrollPane_1.setViewportView(serverSpecificConsole);
		
		startSpecificInfoThread();		// Start the Specific Info Thread
		
		JPanel panel_2 = new JPanel();
		panel_2.setBackground(new Color(143, 188, 143));
		tabbedPane.addTab("Job", null, panel_2, null);
		panel_2.setLayout(null);
		
		JLabel lblSelectJobBy = new JLabel("Select Unprocessed Job:");
		lblSelectJobBy.setBounds(21, 55, 158, 16);
		panel_2.add(lblSelectJobBy);
		
		comboBox_unservicedJobs = new JComboBox();
		comboBox_unservicedJobs.setToolTipText("Click to view and select a unprocessed job");
		comboBox_unservicedJobs.setBounds(191, 50, 192, 27);
		panel_2.add(comboBox_unservicedJobs);
		
		JLabel lblSelectRunningJob = new JLabel("Select Running Job:");
		lblSelectRunningJob.setBounds(21, 21, 137, 16);
		panel_2.add(lblSelectRunningJob);
		
		comboBox_runningJobs = new JComboBox();
		comboBox_runningJobs.setToolTipText("Click to view and select a running job");
		comboBox_runningJobs.setBounds(191, 16, 192, 27);
		panel_2.add(comboBox_runningJobs);
		
		Runnable updateComboBox = new Runnable()
		{
			public void run()
			{
				while(true)
				{					
					// Update the comboBox menu for both kind of jobs
					
					// Update the running jobs
					
					runningJobListCopy.clear();
					unservicedJobListCopy.clear();
					
					for(int i=0;i<correspondingServer.getNPUCount();i++)
					{
						IProcessingUnit tempProcessingUnit = correspondingServer.getProcessingUnit(i);
						
						for(int j=0;j<tempProcessingUnit.getRunningJobCount();j++)
						{
							try {
								runningJobListCopy.add(tempProcessingUnit.getRunningJob(j));
							} catch (Exception e) {
								e.printStackTrace();
								break;
							}
						}
					}
					
					// Update the unserviced jobs
					
					for(int i=0;i<correspondingServer.getDueJobCount();i++)
					{
						try {
							unservicedJobListCopy.add(correspondingServer.getUnservicedJob(i));
						} catch (Exception e) {
							e.printStackTrace();
							break;
						}
					}
					
					String[] runningJobsDropDownMenu = new String[runningJobListCopy.size()];
					String[] unservicedJobsDropDownMenu = new String[unservicedJobListCopy.size()];
					
					// Update the running jobs menu list
					
					for(int i=0;i<runningJobListCopy.size();i++)
						runningJobsDropDownMenu[i] = runningJobListCopy.get(i).getJob().getJobName();
					
					// Update the unserviced jobs menu list
					
					for(int i=0;i<unservicedJobListCopy.size();i++)
						unservicedJobsDropDownMenu[i] = unservicedJobListCopy.get(i).getJobName();
					
					DefaultComboBoxModel unserviced_oldModel = (DefaultComboBoxModel) comboBox_unservicedJobs.getModel();
					DefaultComboBoxModel running_oldModel = (DefaultComboBoxModel) comboBox_runningJobs.getModel();
					
					DefaultComboBoxModel unserviced_newModel = new DefaultComboBoxModel(unservicedJobsDropDownMenu);
					DefaultComboBoxModel running_newModel = new DefaultComboBoxModel(runningJobsDropDownMenu);
					
					boolean running_checkModel = compareModels(running_oldModel,running_newModel);
					boolean unserviced_checkModel = compareModels(unserviced_oldModel,unserviced_newModel);
					
					// Set the comboBox Models
					
					if(running_checkModel == false)
						comboBox_runningJobs.setModel(running_newModel);
					if(unserviced_checkModel == false)
						comboBox_unservicedJobs.setModel(unserviced_newModel);
					
					try {
						Thread.sleep(5000);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			}
		};
		
		Thread updateComboBoxThread = new Thread(updateComboBox);
		updateComboBoxThread.start();
		
		btnShowJob_unserviced = new JButton("Show Job");
		btnShowJob_unserviced.setToolTipText("Click to view details of the selected unprocessed job");
		
		btnShowJob_unserviced.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				
				// Code for showing details of the selected job !!
				
				String jobName = (String) comboBox_unservicedJobs.getSelectedItem();
				
				for(int i=0;i<unservicedJobListCopy.size();i++)		// Search in Unserviced Jobs
				{
					IJob tempJob = unservicedJobListCopy.get(i);
					
					if(tempJob.getJobName().contentEquals(jobName))
					{
						// Update the GUI with this jobs content !!
						
						text_jobDuration.setText(null);
						text_jobDuration.setText(String.valueOf(tempJob.getDuration()));
						
						text_jobMemReq.setText(null);
						text_jobMemReq.setText(String.valueOf(tempJob.getMemoryRequirement()));
						
						text_jobName.setText(null);
						text_jobName.setText(tempJob.getJobName());
						
						text_jobNPE.setText(null);
						text_jobNPE.setText(String.valueOf(tempJob.getNPE()));
						
						text_jobPc.setText(null);
						text_jobPc.setText("N/A");
						
						text_jobPriority.setText(null);
						text_jobPriority.setText(String.valueOf(tempJob.getJobPriority()));
						
						text_JobDesc.setText(null);
						text_JobDesc.setText(tempJob.getJobDescription());
						
						text_jobEndTime.setText(null);
						text_jobEndTime.setText("N/A");
						
						String startTime = "" + tempJob.getStartHour() + ":" + tempJob.getStartMinute() + ":" + tempJob.getStartSecond();
						text_jobStartTime.setText(null);
						text_jobStartTime.setText(startTime);

						break;
					}
				}
			}
		});
		
		btnShowJob_unserviced.setBounds(392, 50, 101, 29);
		panel_2.add(btnShowJob_unserviced);
		
		btnShowJob_running = new JButton("Show Job");
		btnShowJob_running.setToolTipText("Click to view details of the selected running job");
		btnShowJob_running.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) 
			{
				// Add code to show job details
				
				String jobName = (String) comboBox_runningJobs.getSelectedItem();
				
				for(int i=0;i<runningJobListCopy.size();i++)
				{
					IServicedJob tempJob = runningJobListCopy.get(i);
					
					if(tempJob.getJob().getJobName().contentEquals(jobName))
					{
						// Update the GUI with this jobs content !!
						
						text_jobDuration.setText(null);
						text_jobDuration.setText(String.valueOf(tempJob.getJob().getDuration()));
						
						text_jobMemReq.setText(null);
						text_jobMemReq.setText(String.valueOf(tempJob.getJob().getMemoryRequirement()));
						
						text_jobName.setText(null);
						text_jobName.setText(tempJob.getJob().getJobName());
						
						text_jobNPE.setText(null);
						text_jobNPE.setText(String.valueOf(tempJob.getJob().getNPE()));
						
						text_jobPc.setText(null);
						text_jobPc.setText(String.valueOf(tempJob.getPowerConsumed()));
						
						text_jobPriority.setText(null);
						text_jobPriority.setText(String.valueOf(tempJob.getJob().getJobPriority()));
						
						text_JobDesc.setText(null);
						text_JobDesc.setText(tempJob.getJob().getJobDescription());
						
						String endTime = "" + tempJob.getEndHour() + ":" 
						+ tempJob.getEndMinute() + ":" + tempJob.getEndSecond();
						text_jobEndTime.setText(null);
						text_jobEndTime.setText(endTime);
						
						String startTime = "" + tempJob.getJob().getStartHour() + ":" 
						+ tempJob.getJob().getStartMinute() + ":" + tempJob.getJob().getStartSecond();
						text_jobStartTime.setText(null);
						text_jobStartTime.setText(startTime);
						
						break;
					}
				}
			}
		});
		
		btnShowJob_running.setBounds(392, 16, 101, 29);
		panel_2.add(btnShowJob_running);
		
		// Add mouse event HERE !!!!
		
		JSeparator separator = new JSeparator();
		separator.setForeground(new Color(148, 0, 211));
		separator.setBounds(21, 83, 450, 12);
		panel_2.add(separator);
		
		JLabel lblJobName = new JLabel("Job Name:");
		lblJobName.setBounds(80, 107, 71, 16);
		panel_2.add(lblJobName);
		
		JLabel lblMemoryRequirement = new JLabel("Memory Usage (KB's):");
		lblMemoryRequirement.setBounds(80, 135, 137, 16);
		panel_2.add(lblMemoryRequirement);
		
		JLabel lblGivenDurationseconds = new JLabel("Given Duration (Seconds):");
		lblGivenDurationseconds.setBounds(80, 163, 162, 16);
		panel_2.add(lblGivenDurationseconds);
		
		JLabel lblGivenPriority = new JLabel("Given Priority:");
		lblGivenPriority.setBounds(80, 191, 90, 16);
		panel_2.add(lblGivenPriority);
		
		JLabel label_4 = new JLabel("NPE Requirement:");
		label_4.setBounds(80, 219, 111, 16);
		panel_2.add(label_4);
		
		JLabel lblGivenStartTime = new JLabel("Given Start Time:");
		lblGivenStartTime.setBounds(80, 247, 122, 16);
		panel_2.add(lblGivenStartTime);
		
		JLabel lblEstimatedEndTime = new JLabel("Estimated End Time:");
		lblEstimatedEndTime.setBounds(80, 275, 137, 16);
		panel_2.add(lblEstimatedEndTime);
		
		JLabel label_7 = new JLabel("Power Consumption:");
		label_7.setBounds(80, 303, 137, 16);
		panel_2.add(label_7);
		
		text_jobName = new JTextField();
		text_jobName.setText("0");
		text_jobName.setHorizontalAlignment(SwingConstants.CENTER);
		text_jobName.setForeground(Color.WHITE);
		text_jobName.setEditable(false);
		text_jobName.setColumns(10);
		text_jobName.setBackground(new Color(106, 90, 205));
		text_jobName.setBounds(294, 101, 152, 28);
		panel_2.add(text_jobName);
		
		text_jobMemReq = new JTextField();
		text_jobMemReq.setText("0");
		text_jobMemReq.setHorizontalAlignment(SwingConstants.CENTER);
		text_jobMemReq.setForeground(Color.WHITE);
		text_jobMemReq.setEditable(false);
		text_jobMemReq.setColumns(10);
		text_jobMemReq.setBackground(new Color(106, 90, 205));
		text_jobMemReq.setBounds(294, 129, 152, 28);
		panel_2.add(text_jobMemReq);
		
		JLabel lblJobDescription = new JLabel("Job Description:");
		lblJobDescription.setBounds(80, 331, 111, 16);
		panel_2.add(lblJobDescription);
		
		text_jobDuration = new JTextField();
		text_jobDuration.setText("0");
		text_jobDuration.setHorizontalAlignment(SwingConstants.CENTER);
		text_jobDuration.setForeground(Color.WHITE);
		text_jobDuration.setEditable(false);
		text_jobDuration.setColumns(10);
		text_jobDuration.setBackground(new Color(106, 90, 205));
		text_jobDuration.setBounds(294, 157, 152, 28);
		panel_2.add(text_jobDuration);
		
		text_jobPriority = new JTextField();
		text_jobPriority.setText("0");
		text_jobPriority.setHorizontalAlignment(SwingConstants.CENTER);
		text_jobPriority.setForeground(Color.WHITE);
		text_jobPriority.setEditable(false);
		text_jobPriority.setColumns(10);
		text_jobPriority.setBackground(new Color(106, 90, 205));
		text_jobPriority.setBounds(294, 185, 152, 28);
		panel_2.add(text_jobPriority);
		
		text_jobNPE = new JTextField();
		text_jobNPE.setText("0");
		text_jobNPE.setHorizontalAlignment(SwingConstants.CENTER);
		text_jobNPE.setForeground(Color.WHITE);
		text_jobNPE.setEditable(false);
		text_jobNPE.setColumns(10);
		text_jobNPE.setBackground(new Color(106, 90, 205));
		text_jobNPE.setBounds(294, 213, 152, 28);
		panel_2.add(text_jobNPE);
		
		text_jobStartTime = new JTextField();
		text_jobStartTime.setText("0");
		text_jobStartTime.setHorizontalAlignment(SwingConstants.CENTER);
		text_jobStartTime.setForeground(Color.WHITE);
		text_jobStartTime.setEditable(false);
		text_jobStartTime.setColumns(10);
		text_jobStartTime.setBackground(new Color(106, 90, 205));
		text_jobStartTime.setBounds(294, 241, 152, 28);
		panel_2.add(text_jobStartTime);
		
		text_jobEndTime = new JTextField();
		text_jobEndTime.setText("0");
		text_jobEndTime.setHorizontalAlignment(SwingConstants.CENTER);
		text_jobEndTime.setForeground(Color.WHITE);
		text_jobEndTime.setEditable(false);
		text_jobEndTime.setColumns(10);
		text_jobEndTime.setBackground(new Color(106, 90, 205));
		text_jobEndTime.setBounds(294, 269, 152, 28);
		panel_2.add(text_jobEndTime);
		
		text_jobPc = new JTextField();
		text_jobPc.setText("0");
		text_jobPc.setHorizontalAlignment(SwingConstants.CENTER);
		text_jobPc.setForeground(Color.WHITE);
		text_jobPc.setEditable(false);
		text_jobPc.setColumns(10);
		text_jobPc.setBackground(new Color(106, 90, 205));
		text_jobPc.setBounds(294, 297, 152, 28);
		panel_2.add(text_jobPc);
		
		JScrollPane scrollPane_2 = new JScrollPane();
		scrollPane_2.setBounds(294, 331, 152, 53);
		panel_2.add(scrollPane_2);
		
		text_JobDesc = new JTextArea();
		text_JobDesc.setForeground(new Color(255, 255, 255));
		text_JobDesc.setText("My First Job");
		text_JobDesc.setBackground(new Color(123, 104, 238));
		text_JobDesc.setEditable(false);
		scrollPane_2.setViewportView(text_JobDesc);
	}
	
	public JProgressBar getProgressBar() 
	{
		return progressBar;
	}
	
	public JTextField getText_ServerID() {
		return text_ServerID;
	}
	public JTextField getText_ServerNPU() {
		return text_ServerNPU;
	}
	public JTextField getText_ServerCores() {
		return text_ServerCores;
	}
	public JTextField getText_ServerPf() {
		return text_ServerPf;
	}
	public JTextField getText_ServerPc() {
		return text_ServerPc;
	}
	public JTextField getText_ServerMaxPower() {
		return text_ServerMaxPower;
	}
	public JTextField getText_ServerPowAvg() {
		return text_ServerPowAvg;
	}
	public JTextField getText_ServerCap() {
		return text_ServerCap;
	}
	public JTextArea getText_ServerDesc() {
		return text_ServerDesc;
	}
	public JTextArea getServerSpecificConsole() {
		return serverSpecificConsole;
	}
	public JTextField getText_jobsDue() {
		return text_jobsDue;
	}
	public JTextField getText_jobsRunning() {
		return text_jobsRunning;
	}
	
	private void startBasicInfoThread()
	{
		Runnable basicInfoThread = new Runnable()
		{
			public void run()
			{
				while(true)
				{
					// Update all the fields
					
					text_ServerID.setText(null);
					text_ServerID.setText(String.valueOf(correspondingServer.getServerID()));
					
					text_ServerDesc.setText(null);
					text_ServerDesc.setText(correspondingServer.getServerDesc());
					
					text_ServerCap.setText(null);
					text_ServerCap.setText(String.valueOf(sC_UsageData.getServerCapability(correspondingServer.getServerID())));
					
					text_ServerCores.setText(null);
					text_ServerCores.setText(String.valueOf(correspondingServer.getNPECount()));
					
					text_ServerMaxPower.setText(null);
					text_ServerMaxPower.setText(String.valueOf(correspondingServer.getMaxPower()));
					
					text_ServerNPU.setText(null);
					text_ServerNPU.setText(String.valueOf(correspondingServer.getNPUCount()));
					
					text_ServerPc.setText(null);
					text_ServerPc.setText(String.valueOf(correspondingServer.getPowerConsumption()));
					
					text_ServerPf.setText(null);
					text_ServerPf.setText(String.valueOf(correspondingServer.getPowerFactor()));
					
					text_ServerPowAvg.setText(null);
					text_ServerPowAvg.setText(String.valueOf(sC_UsageData.getServerAverage(correspondingServer.getServerID())));
					
					text_ServerMemory.setText(null);
					text_ServerMemory.setText(String.valueOf(correspondingServer.getServerMemory()));
					
					text_ServerFreeMemory.setText(null);
					text_ServerFreeMemory.setText(String.valueOf(correspondingServer.getServerFreeMemory()));
					
					try {
						Thread.sleep(1000);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			}
		};
		
		Thread infoUpdateThread = new Thread(basicInfoThread);
		infoUpdateThread.start();
	}
	
	private void startSpecificInfoThread()
	{
		Runnable specificInfoUpdate = new Runnable()
		{
			public void run()
			{
				while(true)
				{
					// update the 2 fields
					
					text_jobsDue.setText(null);
					text_jobsDue.setText(String.valueOf(correspondingServer.getDueJobCount()));
					
					text_jobsRunning.setText(null);
					text_jobsRunning.setText(String.valueOf(correspondingServer.getRunningJobCount()));
					
					// update the console data
					
					serverSpecificConsoleText = "";
					
					appendToServerConsole("Server Status @ " + Calendar.getInstance().getTime().toString() + "\n");
					appendToServerConsole("No of Processors: " + correspondingServer.getNPUCount());
					
					for(int i=0;i<correspondingServer.getNPUCount();i++)
					{
						appendToServerConsole("\n --- Information for Processor " + correspondingServer.getProcessingUnit(i).getProcessorID() + " ---\n");
						appendToServerConsole("No of Cores: " + correspondingServer.getProcessingUnit(i).getNumberOfProcessingElements());
						appendToServerConsole("Total Processor Memory (KB): " + correspondingServer.getProcessingUnit(i).getTotalMemory());
						appendToServerConsole("Available Free Memory (KB): " + correspondingServer.getProcessingUnit(i).getFreeMemory());
						appendToServerConsole("Current Power Consumption (Watt-Hour): " + correspondingServer.getProcessingUnit(i).getPowerConsumption());
						appendToServerConsole("No of Jobs Running: " + correspondingServer.getProcessingUnit(i).getRunningJobCount());
						appendToServerConsole("Processor System State: " + correspondingServer.getProcessingUnit(i).getSystemState());
					}
					
					clearServerConsole();
					writeToServerConsole(serverSpecificConsoleText);
					
					try {
						Thread.sleep(1000);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			}
		};
		
		Thread specificInfoUpdateThread = new Thread(specificInfoUpdate);
		specificInfoUpdateThread.start();
	}
	
	private void appendToServerConsole(String msg)
	{
		serverSpecificConsoleText = serverSpecificConsoleText + msg + "\n";
	}
	
	private void clearServerConsole()
	{
		getServerSpecificConsole().setText(null);
		getServerSpecificConsole().setCaretPosition(getServerSpecificConsole().getDocument().getLength());
	}
	
	private void writeToServerConsole(String msg)
	{
		getServerSpecificConsole().setText(msg);
	}
	public JTextField getText_ServerMemory() {
		return text_ServerMemory;
	}
	public JTextField getText_jobMemReq() {
		return text_jobMemReq;
	}
	public JTextField getText_jobName() {
		return text_jobName;
	}
	public JComboBox getComboBox() {
		return comboBox_unservicedJobs;
	}
	public JTextField getText_jobDuration() {
		return text_jobDuration;
	}
	public JTextField getText_jobPriority() {
		return text_jobPriority;
	}
	public JTextField getText_jobNPE() {
		return text_jobNPE;
	}
	public JTextField getText_jobStartTime() {
		return text_jobStartTime;
	}
	public JTextField getText_jobEndTime() {
		return text_jobEndTime;
	}
	public JTextField getText_jobPc() {
		return text_jobPc;
	}
	public JTextArea getText_JobDesc() {
		return text_JobDesc;
	}
	public JComboBox getComboBox_runningJobs() {
		return comboBox_runningJobs;
	}
	public JButton getBtnShowJob_unserviced() {
		return btnShowJob_unserviced;
	}
	public JButton getBtnShowJob_running() {
		return btnShowJob_running;
	}
	
	private boolean compareModels(DefaultComboBoxModel oldModel,DefaultComboBoxModel newModel)
	{		
		// Return true if same
		// Return false if different
		
		if(oldModel.getSize() == newModel.getSize())
		{
			// Check for individual elements
			
			for(int i=0;i<oldModel.getSize();i++)
			{
				if(!oldModel.getElementAt(i).equals(newModel.getElementAt(i)))
					return false;
			}
		}
		
		else
			return false;
		
		return true;
	}
	public JTextField getText_ServerFreeMemory() {
		return text_ServerFreeMemory;
	}
}
